// ${code-parser}
// #parse('include.js')
//////////////////////////////////////////////////////////
var ${type}Ctx = new ModuleContext('${TYPE}','#label($model)');

var ${type}UI = new ModuleUI(${type}Ctx);

${type}UI.checkEditForm = function(){
	var vConf = {};
	//#newLine #trim("eachrow", 1, 'rn') #checkConfig('vConf', $model)#end

	var v = new Validator(${type}Ctx.EDIT_FORM_ID, vConf, true);
	var ok = v.validate();
	if(!ok){
		return false;
	}
	return true;
}

${type}UI.setViewItem = function(dataObj, viewObj){
	viewObj.setAttribute('class','row');
	viewObj.setAttribute('id', 'XXXListItem_' + dataObj.id);
	var children = DomUtil.getChildren(viewObj);
#trim("eachrow", 1, 'rn')
#*****显示字段**↓****#
children[0].firstChild.value = dataObj['id'];
#set($fc = 1)
#set($haveFileField = false)
#foreach($field in $MU.findAllField($model))
	#set($mtm = false)
	#set($mtm = $MU.getElementMtm($field))
	#set($isTransient = false)
	#set($isTransient = $field.isTransient)
	#if(!$mtm && !$isTransient)
		#set($mediaType = false)
		#set($mediaType = $MU.getMediaType($field))
		#if($mediaType)
			UploadFileUtil.viewMedia(children[$fc], dataObj['${field.name}'], '$mediaType');
			#set($haveFileField = true)
		#else
			#set($tag = $VU.getInputType($field))
			children[$fc].innerHTML = Widget.getView(dataObj['${field.name}'], '$tag');
		#end
		#set($fc = $fc+1)
	#end
#end

#*****多选对话框按钮**↓****#
#set($mtm=false)
#set($mtmList = $MU.getModelMtmList($model))
#foreach($mtm in $mtmList)
	#set($Model2 = $CSU.getSimpleType($mtm.anotherModel))
	children[$fc].firstChild.onclick = function(){open${Model2}Setup(dataObj.id);};
	#set($fc = $fc+1)
#end

#*****编辑按钮**↓****#
	children[$fc].firstChild.onclick = function(){${type}UI.editXXX(dataObj.id);};
#set($fc = $fc+1)
	children[$fc].firstChild.onclick = function(){${type}UI.deleteXXX(dataObj.id);};
#end

}

#*****ManyToMany**↓↓****#
##
#set($mtmList = $MU.getModelMtmList($model))
#foreach($mtm in $mtmList)
#set($Model2 = $CSU.getSimpleType($mtm.anotherModel))
#set($model2 = $CSU.getVariable($Model2))
#*****打开多选对话框**↓****#
${type}UI.open${Model2}Setup = function(${mtm.relationKey}){
	
	function submitSetup(dialog){
		var url = ${type}Ctx.URL_PREFIX + '/setting/save${Model2}s.json';
		var values = {${mtm.relationKey}:${mtm.relationKey}, ${mtm.anotherRelationKey}:dialog.getSelectedValue()};
		ajaxRequest(url, values, function(result){
			unpopup();
			alert('设置成功！');
		}, 'post');
	}
	
	function openOptions(values){
		var optionsUrl = URL_ROOT+'/${model2}/select.json';
		var optDialog = new OptionDialog('${model2}s-setup', '${model2}Id', 'checkbox', optionsUrl, 'id', 'name');
		optDialog.onOk = submitSetup;
		optDialog.popup('设置', values);
	}
	
	var values = {"${mtm.relationKey}":${mtm.relationKey}};
	var url_get = ${type}Ctx.URL_PREFIX + '/setting/get${Model2}IdList.json';
	ajaxRequest(url_get, values, function(result){
		openOptions(result.data);
	});

}
#end

#if($haveFileField)
${type}UI.setEditFiles = function(dataObj){
	dataObj = dataObj||{}; //添加
#trim("eachrow", 1, 'rn')
	#foreach($field in $MU.findAllField($model))
		#set($mtm = false)
		#set($mtm = $MU.getElementMtm($field))
		#set($isTransient = false)
		#set($isTransient = $field.isTransient)
		#if(!$mtm && !$isTransient)
			#if($MU.haveAtribute($field,"img") || $MU.haveAtribute($field,"audio") || $MU.haveAtribute($field,"video"))
				UploadFileUtil.editFile(dataObj['${field.name}'], $E('${TYPE}EditForm')['$field.name']);
			#end
		#end

	#end
#end

}
#end

EventUtil.addEvent(window, 'load', function(){

	#set($refFieldLst = $MU.getReferenceFields($model))
	#foreach($field in $refFieldLst)
		#set($ref = $field.reference)
		#set($refModel = $CONTEXT.findModel($ref.modelName))
		#set($vRefName = $CSU.getVariable($refModel.name))
#trim('eachrow', 0, 'rn')
		var url_select = URL_ROOT+'/${vRefName}/select.json';
#end
#newLine		ajaxRequest(url_select, null, function(result){
			FormUtil.bindSelectOptions($E('searchForm')['${field.name}'], result.data, 'id', 'name');
			FormUtil.bindSelectOptions($E('${TYPE}EditForm')['${field.name}'], result.data, 'id', 'name');
		});
	#end

});

//////////////////////////////////////////////////////////